﻿Option Strict On
Option Explicit On
Option Infer On

Imports System
Imports System.Collections.Generic
Imports System.Xml.Linq
Imports System.Linq
Imports GenDotNet.Task.Common
Imports GenDotNet.ObjectModel
Imports <xmlns:orm="http://kadgen.com/KADORM.xsd">

Namespace GenInputMetadata
   Public Class PropertyData
      Inherits BaseData
      Implements IPropertyData

      Private mObjectData As IObjectData
      Private mPrivileges As List(Of IPropertyPrivilegeData)

      Private Shared Function Load( _
               ByVal objectData As ObjectData, _
               ByVal element As XElement) _
               As PropertyData
         Dim ret As New PropertyData(objectData.rootdata, element)
         ret.mObjectData = objectData
         ret.mPrivileges = PropertyPrivilegeData.Load(ret, From x In element...<orm:PropertyPrivilege>)
         Return ret
      End Function

      Public Shared Function Load( _
                  ByVal objectData As ObjectData, _
                  ByVal elements As IEnumerable(Of XElement)) _
                  As List(Of IPropertyData)
         Dim ret = New List(Of IPropertyData)
         For Each XElement In elements
            ret.Add(Load(objectData, XElement))
         Next
         Return ret
      End Function

      Private Sub New(ByVal rootData As RootData, _
                      ByVal element As XElement)
         MyBase.New(rootData, element)
      End Sub

      Public ReadOnly Property Name() _
               As String _
               Implements IPropertyData.Name
         Get
            Return Element.@Name
         End Get
      End Property

      Public ReadOnly Property OriginalName() _
               As String _
               Implements IPropertyData.OriginalName
         Get
            Return Element.@OriginalName
         End Get
      End Property

      Public ReadOnly Property IsPrimaryKey() _
               As Boolean _
               Implements IPropertyData.IsPrimaryKey
         Get
            Return Element.@IsPrimaryKey.IsTrue()
         End Get
      End Property

      Public ReadOnly Property NetType() _
               As StandardNetType _
               Implements IPropertyData.NetType
         Get
            Return NetTypeExtensions.XsdToNetType(Element.@ShortNETType)
         End Get
      End Property

      Public ReadOnly Property TableName() _
               As String _
               Implements IPropertyData.TableName
         Get
            Return Element.@TableName
         End Get
      End Property

      Public ReadOnly Property AllowNulls() _
               As Boolean _
               Implements IPropertyData.AllowNulls
         Get
            Return Element.@AllowNulls.IsTrue
         End Get
      End Property

      Public ReadOnly Property Caption() _
               As String _
               Implements IPropertyData.Caption
         Get
            Return Element.@Caption
         End Get
      End Property

      Public ReadOnly Property IsLocalizedProperty() _
               As Boolean _
               Implements IPropertyData.IsLocalizedProperty
         Get
            Return Element.@IsLocalizedProperty.IsTrue
         End Get
      End Property

      Public ReadOnly Property IsReadOnly() _
               As Boolean _
               Implements IPropertyData.IsReadOnly
         Get
            Return Element.@ReadOnly.IsTrue()
         End Get
      End Property

      Public ReadOnly Property MaxLength() _
               As Int32 _
               Implements IPropertyData.MaxLength
         Get
            Return CInt(Element.@MaxLength)
         End Get
      End Property

      Public ReadOnly Property SQLType() _
               As String _
               Implements IPropertyData.SQLType
         Get
            Return Element.@SQLType
         End Get
      End Property

      Public ReadOnly Property ObjectData() As IObjectData Implements IPropertyData.ObjectData
         Get
            Return mObjectData
         End Get
      End Property

      Public ReadOnly Property UseForDesc() As Boolean Implements IPropertyData.UseForDesc
         Get
            Return Element.@UseForDesc.IsTrue
         End Get
      End Property

      Public ReadOnly Property DefaultValue() _
               As String _
               Implements IPropertyData.DefaultValue
         Get
            Dim def = Element.@Default
            If Not String.IsNullOrEmpty(def) Then
               Return def
            ElseIf NetType = StandardNetType.Guid Then
               Return "System.Guid.NewGuid()"
            End If
            Return String.Empty
         End Get
      End Property

      Public ReadOnly Property IsAutoincrement() _
               As Boolean _
               Implements IPropertyData.IsAutoincrement
         Get
            Return Element.@IsAutoIncrement.IsTrue
         End Get
      End Property

      Public ReadOnly Property Privileges() As System.Collections.Generic.List(Of IPropertyPrivilegeData) Implements IPropertyData.Privileges
         Get
            Return mPrivileges
         End Get
      End Property
   End Class
End Namespace
